package com.atguigu.mall.product.service.impl;

import com.atguigu.common.page.PageFactory;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.mall.product.dao.CategoryBrandRelationDao;
import com.atguigu.mall.product.dto.CategoryBrandRelationDTO;
import com.atguigu.mall.product.entity.BrandEntity;
import com.atguigu.mall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.mall.product.entity.CategoryEntity;
import com.atguigu.mall.product.service.BrandService;
import com.atguigu.mall.product.service.CategoryBrandRelationService;
import com.atguigu.mall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
@Transactional(rollbackFor = Exception.class)
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Resource
    private CategoryService categoryService;
    @Resource
    private BrandService brandService;


    @Override
    public PageUtils queryPage(CategoryBrandRelationEntity params) {
        IPage<CategoryBrandRelationEntity> page = PageFactory.defaultPage();
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>();
        Long catelogId = params.getCatelogId();
        wrapper.eq(CategoryBrandRelationEntity::getCatelogId,catelogId);
        this.page(page,wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> queryListByBrandId(Long brandId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId);
        return  this.list(wrapper);
    }

    @Override
    public void saveRelation(CategoryBrandRelationDTO dto) {
        List<Long> categoryIdList = dto.getCategoryIdList();
        Long brandId = dto.getBrandId();
        BrandEntity brand = brandService.getById(brandId);
        String brandName = brand.getName();
        List<CategoryEntity> categoryEntities = categoryService.listByIds(categoryIdList);
        List<CategoryBrandRelationEntity> saveResult = categoryEntities.stream().map(item -> {
            Long catId = item.getCatId();
            String catName = item.getName();
            return CategoryBrandRelationEntity.builder().brandId(brandId).brandName(brandName)
                    .catelogId(catId).catelogName(catName).build();
        }).collect(Collectors.toList());
        this.deleteByBrandId(brandId);
        this.saveBatch(saveResult);
    }

    @Override
    public void deleteByBrandId(Long brandId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId);
        this.remove(wrapper);
    }

    @Override
    public void updateBrandNameByBrandId(Long brandId, String brandName) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> wrapper = new   LambdaUpdateWrapper<>();
        wrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId);
        wrapper.set(CategoryBrandRelationEntity::getBrandName,brandName);
        this.update(wrapper);
    }

    @Override
    public void updateCategoryNameByCategoryId(Long categoryId, String categoryName) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> wrapper = new   LambdaUpdateWrapper<>();
        wrapper.eq(CategoryBrandRelationEntity::getCatelogId,categoryId);
        wrapper.set(CategoryBrandRelationEntity::getCatelogName,categoryName);
        this.update(wrapper);
    }

    @Override
    public List<CategoryBrandRelationEntity> brandsList(Long catId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryBrandRelationEntity::getCatelogId,catId);
        return this.list(wrapper);
    }
}